import requests
import os
import shutil
from util.logger import get_logger
logger = get_logger('中国大学MOOC.log')

try:
    logger.info("程序启动")
    # 创建文件夹
    root = 'static/中国大学MOOC'
    if os.path.exists(root):
        shutil.rmtree(root)
    os.makedirs(root)
    logger.debug("开始获取MOOC的编辑推荐")
    url = 'https://www.icourse163.org/web/j/indexBeanV3.showRecommendCourseForWeb.rpc?csrfKey=e90e7609b85f48fe9cb0208a0edf2d23'

    headers = {
        'content-type':'application/json;charset=UTF-8',
        # 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36',
        # 'Referer':'https://www.icourse163.org/',
        'cookie':'WM_TID=nvgYSHXa1NZAQRARQUbAey0DA6V2CeHC; NTESSTUDYSI=e90e7609b85f48fe9cb0208a0edf2d23; EDUWEBDEVICE=7e0e808333324bd4800e2e7932eb3ede; Hm_lvt_77dc9a9d49448cf5e629e5bebaa5500b=1762095480; HMACCOUNT=04D6CEA720BD4DC4; __yadk_uid=UkRGfOg6lenFXoqFWXni6gTHEZEG47Mr; WM_NI=2hMNnB%2FhEVLd55ffpcuRSrcssc8bYe5S7Psfg7a8tojAotwN2PXXBjOMxD%2FWhAo6omMIx0w9EcDZti6792zY1wBtorzEJib5mMvnkGM7p9emlVFJW3chmu2%2BusdvNxBeV0o%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eed5d44d8bb4a191d963bbb48aa6d55e868f8b82d662958ffdb7f63ff293c0a8c62af0fea7c3b92af2b883afed4aedb88bb7cf70f7ebaa87f369b78e9d93e279b1bdf883dc6ff78df9b1f26ea5e9afd9f765aa9fa195f646f29098b9c1628d93b8a5ae5dac9fa1d7ee678f97b8d1f63387bd8cbbf763f6e9f7d3f953f3a98e95ee7aba9b868ae13faf93ba8fea4d8fe9c0dae867b08a8aabd639a1b8a396b121b7adac97cd7482bb9ad1ee37e2a3; Hm_lpvt_77dc9a9d49448cf5e629e5bebaa5500b=1762095763'
    }

    response = requests.post(url, headers=headers)
    if response.status_code == 200:
        logger.debug("获取MOOC的编辑推荐内容成功")
        items = response.json()['result']
        for item in items:
            if  item['mixedCourseCardVo']['mocCourseBaseCardVo']:
                title = item['mixedCourseCardVo']['mocCourseBaseCardVo']['name']
            else:
                title = item['mixedCourseCardVo']['mocCourseKyCardBulkPurchaseVo']['courseName']
            if  item['mixedCourseCardVo']['mocCourseBaseCardVo']:
                teacher = item['mixedCourseCardVo']['mocCourseBaseCardVo']['teacherName']
            else:
                teacher = item['mixedCourseCardVo']['mocCourseKyCardBulkPurchaseVo']['teacherName']
            if  item['mixedCourseCardVo']['mocCourseBaseCardVo']:
                img_url = item['mixedCourseCardVo']['mocCourseBaseCardVo']['bigPhoto']
            else:
                img_url = item['mixedCourseCardVo']['mocCourseKyCardBulkPurchaseVo']['bigPhoto']
            if item['mixedCourseCardVo']['mocCourseBaseCardVo']:
                school = item['mixedCourseCardVo']['mocCourseBaseCardVo']['schoolName']
            else:
                school = '未知'
            # 保存图片
            logger.debug(f"开始保存{title}的图片")
            img_response = requests.get(img_url)
            if img_response.status_code == 200:
                img_content = img_response.content
                # print(img_content)
                with open(f'{root}/{title}.jpg', 'wb') as f:
                    f.write(img_content)
            logger.debug(f"保存{title}的图片成功")
except Exception as e:
    logger.error(e)
logger.info("运行结束")